#ifndef __PLATFORM_LIB_NET_H__
#define __PLATFORM_LIB_NET_H__
#ifdef __cplusplus
extern "C"
{
#endif

#include <sys/socket.h>

#include "pf_common.h"

/******************************************************************************************************/

typedef struct sockaddr_storage pf_sockaddr_t;

typedef enum pf_family
{
    PF_INETV4 = AF_INET,        /* IPv4 */
    PF_INETV6 = AF_INET6,       /* IPv6 */
} pf_family_e;

/*********************************************************************************************
Function Name:  pf_get_sockport
Description  :  Like htons(), get socket port number from sockaddr.
Inputs          const pf_sockaddr_t* sockaddr : sockaddr pointer. Must be NOT NULL.
Outputs      :  return value                  : > 0 : Port number.
                                                  0 : Fail.
ErrorCodes   :  errno                         : sys call errno from htons()
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern uint16_t pf_get_sockport(const pf_sockaddr_t* sockaddr);

/*********************************************************************************************
Function Name:  pf_get_sockaddr
Description  :  Like sys call inet_notop(), get socket address string from sockaddr.
Inputs       :  pf_sockaddr_t* sockaddr : sockaddr pointer. Must be NOT NULL.
                char* str               : Output string buffer pointer. Must be NOT NULL.
                int32_t str_len         : Length of Output string buffer. Must be greate than zero.
Outputs      :  return value:           : NOT NULL : Success. Pointer to str.
                                              NULL : Fail.
                char* str               : a string in IPv4/IPv6 dotted-decimail notation.
ErrorCodes   :  errno                   : sys call errno from inet_notop().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern char* pf_get_sockaddr(const pf_sockaddr_t* sockaddr, 
                             char* str, 
                             int32_t str_len);

/*********************************************************************************************
Function Name:  pf_listen
Description  :  Bind and listen for connections on a socket.
Inputs       :  int sockfd         : socket fd.
                const char* domain : Host or IP numeric string. Must be NOT NULL.
                uint16_t port      : Port number.
                pf_family_e family : Protocal family.
                uint16_t backlog   : queue length for listen().
Outputs      :  return value       : 0  : Success.
                                     <0 : Fail. Return -(errno).
ErrorCodes   :  errno              : sys call errno.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_listen(int sockfd,
                     const char* domain,
                     uint16_t port, 
                     pf_family_e family,
                     uint16_t backlog);

/*********************************************************************************************
Function Name:  pf_connect
Description  :  Initiate a connection on a socket.
Inputs       :  int sockfd               : socket fd.
                const char* domain       : Host or IP numeric string. Must be NOT NULL.
                uint16_t port            : Port number.
                pf_family_e family       : Protocal family.
                int32_t timeout          : Timeout in milliseconds.
                                           > 0 : Timeout in milliseconds.
                                             0 : Means return immediately.
                                           < 0 : Infinite timeout.
Outputs      :  return value             : 0  : Success.
                                           < 0: Fail. Return -(errno).
ErrorCodes   :  errno                    : sys call errno.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_connect(int sockfd,
                      const char* domain,
                      uint16_t port, 
                      pf_family_e family,
                      int32_t timeout);

/*********************************************************************************************
Function Name:  pf_set_keepalive
Description  :  Enable or disable SO_KEEPALIVE option on a socket.
Inputs       :  int sockfd                    : Socket fd.
                enum PF_OPT opt               : Option value. PF_ON or PF_OFF.
Outputs      :  return value                  : 0  : Success.
                                                -1 : Fail.
ErrorCodes   :  errno                         : sys call errno from setsockopt().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_keepalive(int sockfd, enum PF_OPT opt);

/*********************************************************************************************
Function Name:  pf_set_oobinline
Description  :  Enable or disable SO_OOBINLINE option on a socket.
Inputs       :  int sockfd                    : Socket fd.
                enum PF_OPT opt               : Option value. PF_ON or PF_OFF.
Outputs      :  return value                  : 0  : Success.
                                                -1 : Fail.
ErrorCodes   :  errno                         : sys call errno from setsockopt().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_oobinline(int sockfd, enum PF_OPT opt);

/*********************************************************************************************
Function Name:  pf_set_nodelay
Description  :  Enable or disable TCP_NODELAY option on a socket.
Inputs       :  int sockfd                    : Socket fd.
                enum PF_OPT opt               : Option value. PF_ON or PF_OFF.
Outputs      :  return value                  : 0  : Success.
                                                -1 : Fail.
ErrorCodes   :  errno                         : sys call errno from setsockopt().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_nodelay(int sockfd, enum PF_OPT opt);

/*********************************************************************************************
Function Name:  pf_set_reuseaddr
Description  :  Enable or disable SO_REUSEADDR option on a socket.
Inputs       :  int sockfd                    : Socket fd.
                enum PF_OPT opt               : Option value. PF_ON or PF_OFF.
Outputs      :  return value                  : 0  : Success.
                                                -1 : Fail.
ErrorCodes   :  errno                         : sys call errno from setsockopt().
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_reuseaddr(int sockfd, enum PF_OPT opt);

/*********************************************************************************************
Function Name:  pf_set_linger
Description  :  Set SO_LINGER option on a socket.
Inputs       :  int sockfd              : Socket fd.
                struct linger* linger   : Value of linger to set. Must be NOT NULL.
Outputs      :  return value            :  0 : Success.
                                          <0 : Fail. Return -(errno).
ErrorCodes   :  errno                   : sys call errno from  setsockopt()
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_linger(int sockfd, struct linger* linger);

/*********************************************************************************************
Function Name:  pf_set_recvbuf
Description  :  Set receive buffer size on a socket.
Inputs       :  int sockfd              : Socket fd.
                int32_t buf_size        : Receive buffer size to set.
Outputs      :  return value            : 0  : Success.
                                          <0 : Fail. Return -(errno).
ErrorCodes   :  errno                   : sys call errno from  setsockopt()
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_recvbuf(int sockfd, int32_t buf_size);

/*********************************************************************************************
Function Name:  pf_set_recvbuf
Description  :  Set send buffer size on a socket.
Inputs       :  int sockfd              : Socket fd.
                int32_t buf_size        : Send buffer size to set.
Outputs      :  return value            : 0  : Success.
                                          <0 : Fail. Return -(errno).
ErrorCodes   :  errno                   : sys call errno from  setsockopt()
History      :
---------------------------------------------------------------------------------------------
Author                 Date                       Comments
lijz                   2011-6-24                  create
**********************************************************************************************/
extern int pf_set_sendbuf(int sockfd, int32_t buf_size);

/******************************************************************************************************/

#ifdef __cplusplus
}
#endif
#endif /* #ifndef __PLATFORM_LIB_NET_H__ */


